<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;

//不需要记录日志抛出这个异常
class InvalidRequestException extends Exception
{
    public function __construct(string $message = "", int $code = 200, int $errorCode = 400, $data = [])
    {
        parent::__construct($message, $code);
        $this->errorCode = $errorCode;
        $this->data = $data;
    }

    public function render(Request $request)
    {
        if ($request->expectsJson()) {
            $res = ['code' => $this->errorCode, 'message' => $this->message];
            if (!empty($this->data))
                $res['data'] = $this->data;
            return response()->json($res, $this->code);
        }

        return view('pages.error', ['msg' => $this->message]);
    }
}
